package org.example.myleet.p423;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Solution {
    /**
     * a b c d e f g h i j k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
     * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
     * zero z
     * one n-o
     * two w
     * three r
     * four u
     * five v
     * six x
     * seven s
     * eight g
     * nine n-i
     */
    public String originalDigits(String s) {
        int[] charCount = new int[26];
        for (char c : s.toCharArray()) {
            ++charCount[c - 'a'];
        }
        List<Integer> numbers = new ArrayList<>();
        //z zero
        int zeroCount = charCount[25];
        if (zeroCount > 0) {
            charCount[25] -= zeroCount; //z
            charCount[4] -= zeroCount; //e
            charCount[17] -= zeroCount; //r
            charCount[14] -= zeroCount; //o
            for (int i = 0; i < zeroCount; ++i) {
                numbers.add(0);
            }
        }
        //w two
        int twoCount = charCount[22];
        if (twoCount > 0) {
            charCount[19] -= twoCount; //t
            charCount[22] -= twoCount; //w
            charCount[14] -= twoCount; //o
            for (int i = 0; i < twoCount; ++i) {
                numbers.add(2);
            }
        }
        //u four
        int fourCount = charCount[20];
        if (fourCount > 0) {
            charCount[19] -= fourCount; //f
            charCount[14] -= fourCount; //o
            charCount[20] -= fourCount; //u
            charCount[17] -= fourCount; //r
            for (int i = 0; i < fourCount; ++i) {
                numbers.add(4);
            }
        }
        //r three
        int threeCount = charCount[17];
        if (threeCount > 0) {
            charCount[19] -= threeCount; //t
            charCount[7] -= threeCount; //h
            charCount[17] -= threeCount; //r
            charCount[4] -= 2 * threeCount; //e
            for (int i = 0; i < threeCount; ++i) {
                numbers.add(3);
            }
        }
        //x six
        int sixCount = charCount[23];
        if (sixCount > 0) {
            charCount[18] -= sixCount; //s
            charCount[8] -= sixCount; //i
            charCount[23] -= sixCount; //x
            for (int i = 0; i < sixCount; ++i) {
                numbers.add(6);
            }
        }
        //s seven
        int sevenCount = charCount[18];
        if (sevenCount > 0) {
            charCount[18] -= sevenCount; //s
            charCount[4] -= 2 * sevenCount; //e
            charCount[21] -= sevenCount; //v
            charCount[13] -= sevenCount; //n
            for (int i = 0; i < sevenCount; ++i) {
                numbers.add(7);
            }
        }
        //v five
        int fiveCount = charCount[21];
        if (fiveCount > 0) {
            charCount[5] -= fiveCount; //f
            charCount[8] -= fiveCount; //i
            charCount[21] -= fiveCount; //v
            charCount[4] -= fiveCount; //e
            for (int i = 0; i < fiveCount; ++i) {
                numbers.add(5);
            }
        }
        //g eight
        int eightCount = charCount[6];
        if (eightCount > 0) {
            charCount[4] -= eightCount; //e
            charCount[8] -= eightCount; //i
            charCount[6] -= eightCount; //g
            charCount[7] -= eightCount; //h
            charCount[19] -= eightCount; //t
            for (int i = 0; i < eightCount; ++i) {
                numbers.add(8);
            }
        }
        //n
        if (charCount[13] > 0) {
            //o one
            int oneCount = charCount[14];
            if (oneCount > 0) {
                charCount[14] -= oneCount; //o
                charCount[13] -= oneCount; //n
                charCount[4] -= oneCount; //e
                for (int i = 0; i < oneCount; ++i) {
                    numbers.add(1);
                }
            }
            //i nine
            int nineCount = charCount[8];
            if (nineCount > 0) {
                charCount[13] -= 2 * nineCount; //n
                charCount[8] -= nineCount; //i
                charCount[4] -= nineCount; //e
                for (int i = 0; i < nineCount; ++i) {
                    numbers.add(9);
                }
            }
        }
        numbers.sort(Comparator.naturalOrder());
        StringBuilder sb = new StringBuilder();
        for (int num : numbers) {
            sb.append(num);
        }
        return sb.toString();
    }
}
